<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>区域设置</title>
		<!-- 导入jquery核心类库 -->
		<script type="text/javascript" src="../../js/jquery-1.8.3.js"></script>
		<!--
        	作者：756958289@qq.com
        	时间：2017-08-18
        	描述：导入上传文件的js文件
        -->
		<script type="text/javascript" src="../../js/jquery.ocupload-1.1.2.js"></script>
		<!-- 导入easyui类库 -->
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../js/easyui/themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../js/easyui/ext/portal.css">
		<link rel="stylesheet" type="text/css" href="../../css/default.css">
		<script type="text/javascript" src="../../js/easyui/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.portal.js"></script>
		<script type="text/javascript" src="../../js/easyui/ext/jquery.cookie.js"></script>
		<script src="../../js/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
		<script type="text/javascript">
			var index = undefined;
			function doAdd() {
				if (index != undefined) {
					return;
				};
				var rows = $('#grid').datagrid('getRows');
				if (rows.length > 0) {
					if (rows[0].id == undefined || rows[0].name == undefined ||rows[0].price == undefined) {
						return;
					} 
				}
				$('#grid').datagrid('insertRow',{
				  	index: 0,	// index start with 0
				  	row: {}
				  });
				  index = 0;
				  $("#grid").datagrid('beginEdit',0);
			}
			function doSave() {
				$("#grid").datagrid('endEdit',index);
				
			}
			function doEdit() {
				if (index != undefined) {
					return;
				};
				var row = $("#grid").datagrid('getSelected');
				index = $("#grid").datagrid('getRowIndex',row);
				$("#grid").datagrid('beginEdit',index);
			}
			//取消编辑
			function doDelete() {
				$("#grid").datagrid('cancelEdit',index);
				index =undefined;
			}

			//工具栏
			var toolbar = [{
				id: 'button-edit',
				text: '编辑',
				iconCls: 'icon-edit',
				handler: doEdit
			}, {
				id: 'button-add',
				text: '增加',
				iconCls: 'icon-add',
				handler: doAdd
			},{
				id: 'button-save',
				text: '保存',
				iconCls: 'icon-save',
				handler: doSave
			}, {
				id: 'button-delete',
				text: '取消',
				iconCls: 'icon-cancel',
				handler: doDelete
			}];
			// 定义列
			var columns = [
				[{
					field: 'id',
					title: '编号',
					width: 120,
					align: 'center',
					editor:{
						type: 'validatebox',
						options:{
							required:true
						}
					}
				}, {
					field: 'name',
					title: '名称',
					width: 120,
					align: 'center',
					editor:{
						type: 'validatebox',
						options:{
							required:true
						}
					}
				}, {
					field: 'price',
					title: '价格',
					width: 120,
					align: 'center',
					editor:{
						type: 'validatebox',
						options:{
							required:true
						}
					}
				}],
				
			];

			$(function() {
				// 先将body隐藏，再显示，不会出现页面刷新效果
				$("body").css({
					visibility: "visible"
				});

				// 区域管理数据表格
				$('#grid').datagrid({
					iconCls: 'icon-forward',
					fit: true,
					border: false,
					rownumbers: true,
					striped: true,
					pageList: [10, 20, 30, 50, 100],
					pagination: true,
					toolbar: toolbar,
					url: "../../data/11.json",
					idField: 'id',
					columns: columns,
					singleSelect:true,
					onAfterEdit:function (rowIndex, rowData, changes) {
						index =undefined;
					},
					onCancelEdit:function (rowIndex, rowData) {
						index =undefined;
					}
				});
			})
		</script>
	</head>

	<body class="easyui-layout">
		<div region="center" border="false">
			<table id="grid"></table>
		</div>
	</body>

</html>